package gousermobile

import (
	"encoding/json"
	"fmt"
	block "github.com/corgi-kx/blockchain_golang/blc"
	"github.com/corgi-kx/blockchain_golang/cli"
	"github.com/corgi-kx/blockchain_golang/database"
	"github.com/corgi-kx/blockchain_golang/network"
	log "github.com/corgi-kx/logcustom"
	"os"
	"strings"
)

var Client *cli.Cli

func SelfInit(
	listenHost string,
	rendezvousString string,
	protocolID string,
	listenPort string,
	tokenRewardNum int,
	chineseMnwordPath string,
	thisNodeaddr string,
	wNum int,
	remoteHost string,
	remotePort string,
	logPath string,
	databasePath string,
	bootstrapAddr string,
	bootstrapHost string,
	bootstrapPort string) {

	network.ListenHost = listenHost
	network.RendezvousString = rendezvousString
	network.ProtocolID = protocolID
	network.ListenPort = listenPort
	network.BootstrapAddr = bootstrapAddr
	network.BootstrapHost = bootstrapHost
	network.BootstrapPort = bootstrapPort
	database.ListenPort = listenPort
	database.DataBasePath = databasePath
	block.ListenPort = listenPort
	block.TokenRewardNum = tokenRewardNum
	block.ChineseMnwordPath = chineseMnwordPath
	block.ThisNodeAddr = thisNodeaddr
	block.WNum = wNum

	//cli.RemoteHost = remote_host
	//cli.RemotePort = remote_port
	network.RemoteHost = remoteHost
	network.RemotePort = remotePort

	//创建数据库文件
	//file,err2 := os.OpenFile(fmt.Sprintf("%sblockchain_%s.db", logPath, listenPort), os.O_CREATE|os.O_RDWR, 0777)
	//if err2 != nil {
	//	log.Error(err2)
	//}

	//将日志输出到指定文件
	file, err := os.OpenFile(fmt.Sprintf("%slog%s.txt", logPath, listenPort), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
	if err != nil {
		log.Error(err)
	}
	log.SetOutputAll(file)

	Client = cli.New()
	Client.RunBackend()
}

func GetBalance(address string) string {
	b := network.GetBalance(address)
	return b
}

func GenerateWallet() string {
	bd := database.New()
	wallets := block.NewWallets(bd)
	address, privkey, mnemonicWord := wallets.GenerateWallet(bd, block.NewBitcoinKeys, []string{})
	return mnemonicWord + "|" + privkey + "|" + address
}

func Transfer(from, to, amount string) string {
	fromString := strings.TrimSpace(from)
	toString := strings.TrimSpace(to)
	amountString := strings.TrimSpace(amount)
	var tss []block.Transaction
	blc := block.NewBlockchain()
	tss = blc.CreateTransaction(fromString, toString, amountString, network.Send{})
	tssbytes := network.SerializeTransactions(tss)
	_, err := network.HttpChangeData("post_transactions", tssbytes)
	if err != nil {
		return "转账失败"
	} else {
		log.Infof("执行转账指令from %s,to %s,amount %s", from, to, amount)
		return from + "->" + to + ":" + amount
	}
}

func ImportWallet(wordsList string) string {
	mnemonicwords := []string{}
	err := json.Unmarshal([]byte(wordsList), &mnemonicwords)
	if err != nil {
		log.Error("json err:", err)
		return "钱包导入错误"
	}

	bd := database.New()
	wallets := block.NewWallets(bd)
	address, privkey, mnemonicWord := wallets.GenerateWallet(bd, block.CreateBitcoinKeysByMnemonicWord, mnemonicwords)
	return mnemonicWord + "|" + privkey + "|" + address
}

func SetRewardAddr(addr string) {
	bc := block.NewBlockchain()
	bc.SetRewardAddress(addr)
}

func ToLogInfo(info string) {
	log.Info(info)
}
